#include "PPM.h"


PPM_val PPM_FLIGHT;
PPM_CONTROL PPM_con;
u8  TIM3CH1_CAPTURE_STA=0;	//���벶��״̬
u16	TIM3CH1_CAPTURE_Date2;	//����2
u16 TIM3CH1_CAPTURE_Date1;	//����1
//CH2
u8  TIM3CH2_CAPTURE_STA=0;	//���벶��״̬
u16	TIM3CH2_CAPTURE_Date2;	//����2
u16 TIM3CH2_CAPTURE_Date1;	//����1
//CH3
u8  TIM3CH3_CAPTURE_STA=0;	//���벶��״̬
u16	TIM3CH3_CAPTURE_Date2;	//����2
u16 TIM3CH3_CAPTURE_Date1;	//����1
//CH4
u8  TIM3CH4_CAPTURE_STA=0;	//���벶��״̬
u16	TIM3CH4_CAPTURE_Date2;	//����2
u16 TIM3CH4_CAPTURE_Date1;	//����1

void PPM1_init(void)
{
//    PPM1_init(0XFFFFFFFF,84-1);//以1Mhz的频率计数
    PPM_FLIGHT.T5_CAP4_STA=0;
    PPM_FLIGHT.T5_CAP3_STA=0;
    PPM_FLIGHT.T5_CAP2_STA=0;
    PPM_FLIGHT.T3_CAP1_STA=0;

    TIM3_CLOCK_INIT;
    RCC->AHB1ENR|=1<<0;     //使能PORTA时钟
    RCC->AHB1ENR|=1<<1;     //使能PORTB时钟
    GPIO_Set(GPIOA,PIN6|PIN7,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PD);//复用功能,下拉
    GPIO_Set(GPIOB,PIN0|PIN1,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PD);//复用功能,下拉
    GPIO_AF_Set(GPIOA,6,2); //PA6,AF2 T3-CH1-4 yaw      对应接收机通道1
    GPIO_AF_Set(GPIOA,7,2); //PA7,AF2 T3-CH2-3 roll     对应接收机通道2
    GPIO_AF_Set(GPIOB,0,2); //PB0,AF2 T3-CH3-2 pitch    对应接收机通道4
    GPIO_AF_Set(GPIOB,1,2); //PB1,AF2 T3-CH4-1 thrust   对应接收机通道3

    TIM3->ARR=0XFFFFFFFF;          //设定计数器自动重装值
    TIM3->PSC=83;          //预分频器

    TIM3->CCMR1|=0x101;
    TIM3->CCMR2|=0x101;
    TIM3->CCER|=0x1111;
    TIM3->EGR=1<<0;         //软件控制产生更新事件,使写入PSC的值立即生效,否则将会要等到定时器溢出才会生效!
    TIM3->DIER|=0x1F;       //允许捕获1-4中断
//    TIM3->CCER|=0<<1;       //CC1P=0    上升沿捕获
//    TIM3->CCER|=1<<0;       //CC1E=1    允许捕获计数器的值到捕获寄存器中0;      //允许更新中断
    TIM3->CR1|=0x01;        //使能定时器3
    MY_NVIC_Init(2,0,TIM3_IRQn,2);//抢占2，子优先级0，组2
    //PPM信号指示灯
//    RCC->AHB1ENR|=1<<2;//使能PORTC时钟
//    RCC->AHB1ENR|=1<<1;//使能PORTB时钟
//    GPIO_Set(GPIOC,PIN1,GPIO_MODE_OUT,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU); //PC13设置
//    GPIO_Set(GPIOB,PIN2,GPIO_MODE_OUT,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);  //PB2设置

//    MX_TIM3_Init();
    PPM_FLIGHT.power_sw = 0;
    rt_kprintf("PPM_init is ok!  motor power switch  is %d \n",PPM_FLIGHT.power_sw);
}
//INIT_APP_EXPORT(PPM1_init);
//��ʱ��5�жϷ������
void TIM3_IRQHandler(void)
{ 		    
	u16 tsr;
	tsr=TIM3->SR;
    //CH1中断处理
    if((TIM3CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
    {
        if(tsr&0X01)//溢出
        {
            if(TIM3CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
            {
                if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
                {
                    TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次
                    TIM3CH1_CAPTURE_Date2=0XFFFF;
                }else TIM3CH1_CAPTURE_STA++;
            }
        }
        if(tsr&0x02)//捕获1发生捕获事件
        {
            if(TIM3CH1_CAPTURE_STA&0X40)        //捕获到一个下降沿
            {
                TIM3CH1_CAPTURE_STA|=0X80;      //标记成功捕获到一次高电平脉宽
                TIM3CH1_CAPTURE_Date2=TIM3->CCR1;   //获取当前的捕获值.
                TIM3->CCER&=~(1<<1);            //CC1P=0 设置为上升沿捕获
            }else                               //还未开始,第一次捕获上升沿
            {
                TIM3CH1_CAPTURE_Date2=0;
                TIM3CH1_CAPTURE_STA=0X40;       //标记捕获到了上升沿
                TIM3CH1_CAPTURE_Date1=TIM3->CCR1;
                TIM3->CCER|=1<<1;               //CC1P=1 设置为下降沿捕获
            }
        }
    }
    else
    {
            PPM_FLIGHT.PPM1_RES=TIM3CH1_CAPTURE_STA&0X3F;
            PPM_FLIGHT.PPM1_RES*=0xFFFF;                        //溢出时间总和
            PPM_FLIGHT.PPM1_RES+=TIM3CH1_CAPTURE_Date2;
            PPM_FLIGHT.PPM1_RES-=TIM3CH1_CAPTURE_Date1;     //得到总的高电平时间
            TIM3CH1_CAPTURE_Date1=0;
            TIM3CH1_CAPTURE_STA=0;              //开启下一次捕获

            if(PPM_FLIGHT.PPM1_RES<2000)
            {
                PPM_FLIGHT.PPM1 = PPM_FLIGHT.PPM1_RES;
                PPM_con.PPM_yaw = (float)PPM_FLIGHT.PPM1*0.06098-90.24 ;
                if(fabs(PPM_con.PPM_yaw)<2)PPM_con.PPM_yaw=0;
//                if(JY901_data.con_yaw>27 && JY901_data.con_roll > 27) msh_sta.PPM_con_sta =1;
//                else if (JY901_data.con_yaw< -27 && JY901_data.con_roll < -27)msh_sta.PPM_con_sta =0;
                //*******左右两杆靠外 msh_sta.PPM_con_sta =0;启动遥控器速度环控制
                //*******左右两杆靠内 msh_sta.PPM_con_sta =1;启动遥控器角度环控制
            }

    }
    //CH2中断处理
    if((TIM3CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
    {
        if(tsr&0X01)//溢出
        {
            if(TIM3CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
            {
                if((TIM3CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
                {
                    TIM3CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次
                    TIM3CH2_CAPTURE_Date2=0XFFFF;
                }else TIM3CH2_CAPTURE_STA++;
            }
        }
        if(tsr&0x04)//捕获1发生捕获事件
        {
            if(TIM3CH2_CAPTURE_STA&0X40)        //捕获到一个下降沿
            {
                TIM3CH2_CAPTURE_STA|=0X80;      //标记成功捕获到一次高电平脉宽
                TIM3CH2_CAPTURE_Date2=TIM3->CCR2;   //获取当前的捕获值.
                TIM3->CCER&=~(1<<5);            //CC1P=0 设置为上升沿捕获
            }else                               //还未开始,第一次捕获上升沿
            {
                TIM3CH2_CAPTURE_Date2=0;
                TIM3CH2_CAPTURE_STA=0X40;       //标记捕获到了上升沿
                TIM3CH2_CAPTURE_Date1=TIM3->CCR2;
                TIM3->CCER|=1<<5;               //CC1P=1 设置为下降沿捕获
            }
        }
    }
    else
    {
            PPM_FLIGHT.PPM2_RES=TIM3CH2_CAPTURE_STA&0X3F;
            PPM_FLIGHT.PPM2_RES*=0xFFFF;                        //溢出时间总和
            PPM_FLIGHT.PPM2_RES+=TIM3CH2_CAPTURE_Date2;
            PPM_FLIGHT.PPM2_RES-=TIM3CH2_CAPTURE_Date1;     //得到总的高电平时间
            TIM3CH2_CAPTURE_Date1=0;
            TIM3CH2_CAPTURE_STA=0;              //开启下一次捕获

            if(PPM_FLIGHT.PPM2_RES<2000)
            {
                PPM_FLIGHT.PPM2 = PPM_FLIGHT.PPM2_RES;
                PPM_con.PPM_roll = (float)PPM_FLIGHT.PPM2*0.06098-90.24 ;
                PPM_con.PPM_roll = -PPM_con.PPM_roll ;
                if(fabs(PPM_con.PPM_roll)<2)PPM_con.PPM_roll=0;
            }

    }
    //CH3中断处理
    if((TIM3CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
    {
        if(tsr&0X01)//溢出
        {
            if(TIM3CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
            {
                if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
                {
                    TIM3CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
                    TIM3CH3_CAPTURE_Date2=0XFFFF;
                }else TIM3CH3_CAPTURE_STA++;
            }
        }
        if(tsr&0x08)//捕获1发生捕获事件
        {
            if(TIM3CH3_CAPTURE_STA&0X40)        //捕获到一个下降沿
            {
                TIM3CH3_CAPTURE_STA|=0X80;      //标记成功捕获到一次高电平脉宽
                TIM3CH3_CAPTURE_Date2=TIM3->CCR3;   //获取当前的捕获值.
                TIM3->CCER&=~(1<<9);            //CC1P=0 设置为上升沿捕获
            }else                               //还未开始,第一次捕获上升沿
            {
                TIM3CH3_CAPTURE_Date2=0;
                TIM3CH3_CAPTURE_STA=0X40;       //标记捕获到了上升沿
                TIM3CH3_CAPTURE_Date1=TIM3->CCR3;
                TIM3->CCER|=1<<9;               //CC1P=1 设置为下降沿捕获
            }
        }
    }
    else
    {
            PPM_FLIGHT.PPM3_RES=TIM3CH3_CAPTURE_STA&0X3F;
            PPM_FLIGHT.PPM3_RES*=0xFFFF;                        //溢出时间总和
            PPM_FLIGHT.PPM3_RES+=TIM3CH3_CAPTURE_Date2;
            PPM_FLIGHT.PPM3_RES-=TIM3CH3_CAPTURE_Date1;     //得到总的高电平时间
            TIM3CH3_CAPTURE_Date1=0;
            TIM3CH3_CAPTURE_Date2=0;
            TIM3CH3_CAPTURE_STA=0;              //开启下一次捕获
            if(PPM_FLIGHT.PPM3_RES<2000)
            {
                PPM_FLIGHT.PPM3 = PPM_FLIGHT.PPM3_RES;
                PPM_con.PPM_pitch = (float)PPM_FLIGHT.PPM3*0.06098-90.24 ;
                if(fabs(PPM_con.PPM_pitch)<2)PPM_con.PPM_pitch=0;
            }
    }
	//CH4�жϴ���
	if((TIM3CH4_CAPTURE_STA&0X80)==0)//��δ�ɹ�����
	{
		if(tsr&0X01)//���
		{	 
			if(TIM3CH4_CAPTURE_STA&0X40)//�Ѿ����񵽸ߵ�ƽ��
			{
				if((TIM3CH4_CAPTURE_STA&0X3F)==0X3F)//�ߵ�ƽ̫����
				{
					TIM3CH4_CAPTURE_STA|=0X80;//��ǳɹ�������һ��
					TIM3CH4_CAPTURE_Date2=0XFFFF;
				}else TIM3CH4_CAPTURE_STA++;
			}	 
		}
		if(tsr&0x10)//����1���������¼�
		{	
			if(TIM3CH4_CAPTURE_STA&0X40)		//����һ���½���
			{	  			
				TIM3CH4_CAPTURE_STA|=0X80;		//��ǳɹ�����һ�θߵ�ƽ����
			    TIM3CH4_CAPTURE_Date2=TIM3->CCR4;	//��ȡ��ǰ�Ĳ���ֵ.
	 			TIM3->CCER&=~(1<<13);			//CC1P=0 ����Ϊ�����ز���
			}else  								//��δ��ʼ,��һ�β���������
			{ 
				TIM3CH4_CAPTURE_Date2=0;
				TIM3CH4_CAPTURE_STA=0X40;		//��ǲ�����������
				TIM3CH4_CAPTURE_Date1=TIM3->CCR4;
				TIM3->CCER|=1<<13; 				//CC1P=1 ����Ϊ�½��ز���
			}		    
		}			     	    					   
 	}
	else
	{		
			PPM_FLIGHT.PPM4_RES=TIM3CH4_CAPTURE_STA&0X3F;
			PPM_FLIGHT.PPM4_RES*=0xFFFF;						//���ʱ���ܺ�
			PPM_FLIGHT.PPM4_RES+=TIM3CH4_CAPTURE_Date2;
			PPM_FLIGHT.PPM4_RES-=TIM3CH4_CAPTURE_Date1;		//�õ��ܵĸߵ�ƽʱ��
			TIM3CH4_CAPTURE_Date1=0;
 			TIM3CH4_CAPTURE_STA=0;				//������һ�β���

			if(PPM_FLIGHT.PPM4_RES<2010)
			{
                PPM_FLIGHT.PPM4 = PPM_FLIGHT.PPM4_RES ;
//                PPM_con.PPM_thrust = (float)PPM_FLIGHT.PPM4-1000;
// //               PPM_con.PPM_thrust = (float)PPM_FLIGHT.PPM4*1.014-1011 ;
//                if(PPM_con.PPM_thrust<5)PPM_con.PPM_thrust=0;
//                if(PPM_con.PPM_thrust>1000)PPM_con.PPM_thrust=1000;
////                control.thrust=JY901_data.con_thrust;
////                control.thr_min = PPM_con.PPM_thrust;
//                if(PPM_con.PPM_thrust>20)PPM_FLIGHT.power_sw =1;
//                else PPM_FLIGHT.power_sw=0;
//                led_Y =! led_Y;
			}
	}

	TIM3->SR=0;//����жϱ�־λ
}
